
import 'package:aweb5/generated/json/member_info_entity_helper.dart';
import 'package:aweb5/pages/plan_detail.dart';
import 'package:aweb5/util/DateUtil.dart';
import 'package:flutter/material.dart';
import 'pages/AplusHomePage.dart';
import 'util/fluwxUtil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'pages/AplusLoginPage.dart';
import 'pages/AplusLoginWithPhone.dart';
import 'pages/RegisterWithPhone.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'pages/AplusRegisterSuccess.dart';
import 'package:aweb5/bean/aplus/member_info_entity.dart';
import 'package:aweb5/util/SharedUtil.dart';
import 'Constants.dart';
import 'util/JsonUtil.dart';
import 'package:load/load.dart';
//import 'pages/AplusHomePageVflutterwebviewplugin.dart';
import 'package:fluwx/fluwx.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'pages/choose_mission.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'pages/registery.dart';
import 'pages/today_plan.dart';
import 'pages/choose_mission.dart';
import 'pages/material_lib.dart';
import 'pages/plan_material.dart';
import 'pages/view_somebody_and_officialassist_plan.dart';
import 'pages/view_official_yunying_plan.dart';
import 'package:aweb5/pages/product_list.dart';
import 'package:provider/provider.dart';
import 'providerdata/GroupNoti.dart';
import 'package:aweb5/util/LogUtil.dart';
void main() {


  LogUtil.init(title: "来自LogUtil", isDebug: true,limitLength:80000);
  WidgetsFlutterBinding.ensureInitialized();

  runApp(
//      LoadingProvider(
//    child:
//    MultiProvider(
//      providers: [
//        ChangeNotifierProvider(create: (_) => GroupNoti()),
//      ],
//    child:

    MyApp(),

//    )
//    ,
//  )
  );

}



class MyApp extends StatelessWidget {



  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    DateTime dateTime = DateTime.now();
//    print("datetime>>>>>${dateTime.}");
    return
//      FlutterEasyLoading(
//      child:
    MaterialApp(
        title: '集佳好物',
        theme: ThemeData(

          primarySwatch: Colors.blue,
        ),
      home:
      LoadingProvider(child:
      RootApp(),
//      Temp()
      ),

//        home: Temp(),

        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        locale: const Locale("en", "US"),
        supportedLocales: [
          const Locale("zh", "CH"),
          const Locale("en", "US")
        ],
      )
//    )
    ;



      ;
  }
}





class RootApp extends StatefulWidget {
  @override
  _RootAppState createState() => _RootAppState();
}

class _RootAppState extends State<RootApp> {
  Widget rootWidget = Scaffold();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _initFluwx();
    reouter();

  }
  _initFluwx() async {
    await registerWxApi(
//        appId: "wx8d3746380a937886",
        appId: "wxb62333ccba1ac180",
        doOnAndroid: true,
        doOnIOS: true,
        universalLink: "https://a.91aplus.com/");
    var result = await isWeChatInstalled;
    print("is installed $result");
//    Fluttertoast.showToast(msg: "is installed $result");
  }
  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 414, height: 896);
    return  rootWidget;
  }

   reouter() async{

     String memberInfo= await SharedUtil.query(Constants.MEMBER_INFO);
     String lastLoginTime= await SharedUtil.query(Constants.LAST_LOGIN_TIME);
     bool show = await getFirst();

      if(memberInfo==null||lastLoginTime ==null){
        show = show==null?true:false;
        rootWidget = LoginWithPhone(show);
      }else{

        SharedUtil.addStr(Constants.LAST_LOGIN_TIME, DateTime.now().toString());
        MemberInfoEntity memberInfoEntity  = new MemberInfoEntity();
        Constants.memberInfoEntity = memberInfoEntityFromJson(memberInfoEntity, JsonUtil.json2map(memberInfo));
        rootWidget = HomePage(Constants.memberInfoEntity.data.memberId.toString());
//        rootWidget = AplusHomePageVflutterwebviewplugin(Constants.memberInfoEntity.data.memberId.toString());

      }
      setState(() {

      });



  }
}




class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);



  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {

      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(

          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }




}

Future<bool> getFirst() async{
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool s = prefs.getBool("first");
  if(s==null){
    print("null");
  }else{
    print(s);
  }
  return s;
}


class Temp extends StatefulWidget {
  @override
  _TempState createState() => _TempState();
}

class _TempState extends State<Temp> {

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 414, height: 896);
    return PlanMaterial();
//    return ChooseMission();
//    return ViewOfficialYunYingPlan();
//    return ViewSomebodyAndOfficialAssistPlan();
//  return PlanDetail();
//    return Todayplan();
//  return ProductList();
//  return Reigistry();
  }
}
